package cn.signalpha.modules.com.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.CollectionUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.com.vo.UserVO;
import cn.signalpha.modules.com.wrapper.UserWrapper;
import cn.signalpha.modules.shop.service.OrderService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_USER_NAME + "/sys-seller/user")
@Api(value = "用户", tags = "用户")
public class SpiUserController extends BaseController {

	private final UserService userService;
	private final OrderService orderService;

	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="mobile", dataType = "string", value = "电话"),
		@ApiImplicitParam(name="nickName", dataType = "string", value = "昵称"),
		@ApiImplicitParam(name="status", dataType = "string", value = "状态"),
		@ApiImplicitParam(name="level", dataType = "string", value = "等级"),
	})
	@ApiOperation(value = "分页", notes = "")
	public R<IPage<UserVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		List<Long> userIds = orderService.selectUserIdBySellerId(getUser().getUserId());
		if(CollectionUtil.isEmpty(userIds)){
			return R.data(new Page<>());
		}
		params.put("ids", Func.join(userIds));
		return R.data(userService.selectUserPage(Condition.getPage(query), params));
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<User> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(UserWrapper.build().entityVO(userService.getById(id)));
	}

}
